package test.wechat.service;

import java.util.ArrayList;
import java.util.List;

import lodsve.wechat.api.menu.WeChatMenuService;
import lodsve.wechat.api.template.WeChatTemplateService;
import lodsve.wechat.api.user.WeChatUserService;
import lodsve.wechat.beans.*;
import lodsve.wechat.enums.Lang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * .
 *
 * @author sunhao(sunhao.java@gmail.com)
 * @version V1.0, 16/2/25 下午12:00
 */
@Service
public class WeixinService {
    @Autowired
    private WeChatMenuService menuService;
    @Autowired
    private WeChatTemplateService templateService;
    @Autowired
    private WeChatUserService weChatUserService;

    public List<Menu> getMenus() {
        return menuService.getMenus();
    }

    public void saveMenus(List<Menu> menus) {
        menus.addAll(getMenus());
        menuService.create(menus.toArray(new Menu[menus.size()]));
    }

    public void deleteMenus() {
        menuService.delete();
    }

    public void setIndustry(int industryId1, int industryId2) {
        templateService.setIndustry(industryId1, industryId2);
    }

    public Industry getIndustry() {
        return templateService.getIndustry();
    }

    public String getTemplateId(String templateShortId) {
        return templateService.getTemplateId(templateShortId);
    }

    public List<Template> listAllTemplates() {
        return templateService.listAllTemplates();
    }

    public void deleteTemplate(String templateId) {
        templateService.deleteTemplate(templateId);
    }

    public void sendTemplate(String toUser, String templateId, String url, List<TemplateData> datas) {
        templateService.sendTemplateMsg(toUser, templateId, url, datas);
    }

    public void updateUserAlias(String openId, String alias) {
        weChatUserService.updateUserAlias(openId, alias);
    }

    public User getUser(String openId, Lang lang) {
        return weChatUserService.getUser(openId, lang);
    }

    public List<User> listUsers() {
        List<String> openIds = weChatUserService.listSubscribe(null);
        List<UserQuery> users = new ArrayList<>(openIds.size());
        for (String openId : openIds) {
            UserQuery user = new UserQuery();
            user.setOpenId(openId);
            user.setLanguage(Lang.zh_CN);

            users.add(user);
        }

        return weChatUserService.batchGetUser(users);
    }
}
